package com.chapter16.exit_;

/**
 * @author 宋鑫宇
 * @version 1.0
 */
public class ThreadExit_ {
    public static void main(String[] args) {
        T t = new T();
        t.start();
        //如果希望main线程去控制t线程的终止，需要在主线程修改loop
        //让t 退出run方法，从而终止t 线程（通知方式）
        try {
            Thread.sleep(10*1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t.setLoop(false);
    }
}

class T extends Thread {
    private boolean loop = true;
    @Override
    public void run() {
        while (loop) {
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "....");
        }
    }

    public void setLoop(boolean loop) {
        this.loop = loop;
    }
}
